package com.maijinjie.admin.sys.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maijinjie.admin.sys.bean.UserBean;
import com.maijinjie.admin.sys.service.UserService;
import com.maijinjie.common.action.BaseAction;
import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.session.SessionContext;
import com.maijinjie.common.util.RequestUtil;

/**
 * 用户登录
 * @version 1.0
 * @author GhostarX ( 王欣逸 ) 100992995@qq.com
 * @data 2013-8-19 下午2:16:02
 */
@Controller
@RequestMapping("/")
public class LoginAction extends BaseAction{
	@Resource
	UserService userService;
	/**
	 * 登录
	 * @param model
	 * @param pagination
	 * @param userName
	 * @param password
	 * @param req
	 * @return
	 * @throws Exception
	 * @version 1.0
	 * @author GhostarX ( 王欣逸 ) 100992995@qq.com 18902499340
	 * @data 2013-8-19 下午2:15:38
	 */
	@RequestMapping(value="/login")
	public String login(String userName, String password,
			Model model,Pagination<UserBean> pagination, HttpServletRequest req) throws Exception{
	    UserBean bean = userService.listByLogin(userName, password);
		if(null==bean){
			model.addAttribute("msg","用户名或密码错误！");
			return "/login";
		} 
//		else if(1==bean.getIsStop()){
//			model.addAttribute("msg","该账户已停用！");
//			return "/login";
//		}
	else {	
			SessionContext.setUser(bean, req);//保存登录用户信息
//			sysUserService.updateLoginIpByUserId(RequestUtil.getIP(req), bean.getId());
			return "/admin/main";
		}
	}
	/**
	 * 退出
	 * @return
	 * @throws Exception
	 * @version 1.0
	 * @author GhostarX ( 王欣逸 ) 100992995@qq.com 18902499340
	 * @data 2013-8-19 下午2:15:44
	 */
	@RequestMapping(value="/exit")
	public String exit(HttpServletRequest req) throws Exception{
		SessionContext.removeUser(req);
		return "/jsp/login";
	}
}